from lxml import etree
import urllib.request as ur
import user_agent


def getRequest(url):
    return ur.Request(
        url=url,
        headers={
            'User-Agent':user_agent.get_user_agent_pc(),
        }
    )

request = getRequest(
    'https://maoyan.com/board'
)

response = ur.urlopen(request).read()

with open('猫眼1.html', 'wb')as f:
    f.write(response)

with open('猫眼1.html','r',encoding='utf-8')as f:
    content = f.read()

# 转化格式
tree = etree.HTML(content)


# 确定根
dd_list = tree.xpath('//dd')

#print(dd_list)

for dd in dd_list:

    #标题
    title = dd.xpath('.//p[@class="name"]/a/text()')[0]
    print(title)

    # 主演员
    actor = dd.xpath('.//p[@class="star"]/text()')[0]
    actor = actor.strip()
    print(actor)

    # 评分
    score_list = dd.xpath('.//p[@class="score"]/i/text()')
    print(score_list)
    score = ''.join(score_list)
    print(score)

    # 图片
    picture = dd.xpath('.//a/img/@data-src')[0]
    print(picture)